package com.atguigu.eduservice.controller;


import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.service.EduChapterService;
import common.atguigu.commonutils.R;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-06-01
 */
@RestController
@RequestMapping("/eduservice/chapter")
@CrossOrigin
public class EduChapterController {

    @Autowired
    private EduChapterService chapterService;

    //根据课程id查询课程大纲列表
    @GetMapping("/getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable String courseId) {
        List<ChapterVo> list = chapterService.getChapterVideoByCourseId(courseId);
        return R.ok().data("allChapterVideo", list);
    }

    //添加章节
    @PostMapping("/addChapter")
    private R addChapter(@RequestBody EduChapter eduChapter) {
        boolean save = chapterService.save(eduChapter);
        return R.ok().message("添加章节成功");
    }

    //根据章节id查询章节信息
    @GetMapping("/getChapterInfo/{chapterId}")
    private R getChapterInfo(@PathVariable String chapterId) {
        EduChapter chapter = chapterService.getById(chapterId);
        return R.ok().message("查询成功").data("chapter", chapter);
    }

    //修改章节
    @PostMapping("/updateChapter")
    private R updateChapter(@RequestBody EduChapter eduChapter) {
        chapterService.updateById(eduChapter);
        return R.ok().message("修改章节成功");
    }

//    //删除章节的方法  当有小节时不删除
//    @DeleteMapping("deleteChapter/{chapterId}")
//    private R deleteChapter(@PathVariable String chapterId) {
//        Boolean flag = chapterService.deleteChapter(chapterId);
////        if (flag) {
////            return R.ok();
////        } else {
////            return R.ok();
////        }
//        return flag ? R.ok().message("删除成功"):R.error().message("删除失败，有小节");
//    }

    @ApiOperation("删除章节：包括章节下所有的小节以及小节里的视频")
    @DeleteMapping("deleteChapter/{chapterId}")
    public R deleteChapter(@ApiParam(name="chapterId",value = "章节ID",required = true)
                           @PathVariable String chapterId){
        Boolean deleteChapter=  chapterService.deleteChapter(chapterId);
        return deleteChapter?  R.ok().message("删除成功"):R.error().message("删除失败，有小节");
    }
}

